﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CopyIt
{
    public partial class DisplayPreview : UserControl
    {
        public event EventHandler Close;
        public event EventHandler Copy;
        private Syncronizer sincronizer = null;
        public TreeView Tree { get { return this.treeView1; } }

        public delegate void AddNodeDelegate(object sender, SyncronizerEventArgs e);
        AddNodeDelegate AddNode;

        public DisplayPreview()
        {
            InitializeComponent();
            sincronizer = new Syncronizer();
            sincronizer.FileSyncronize += new Syncronizer.SyncronizeHandler(sincronizer_FileSyncronize);
            sincronizer.BeginSyncronize += new EventHandler(sincronizer_BeginSyncronize);
            sincronizer.EndSyncronize += new EventHandler(sincronizer_EndSyncronize);
            sincronizer.AddNode += new Syncronizer.SyncronizeHandler(sincronizer_AddNode);
            AddNode = new AddNodeDelegate(sincronizer_AddNode);
        }

        void sincronizer_AddNode(object sender, SyncronizerEventArgs e)
        {
            if (treeView1.InvokeRequired)
                treeView1.BeginInvoke(AddNode, new Object[] { sender, e });
            else
            {
                treeView1.BeginUpdate();
                treeView1.Nodes.Add(e.Node);
                treeView1.EndUpdate();
            }
        }

        void sincronizer_EndSyncronize(object sender, EventArgs e)
        {
            tsbStop.Visible = false;
            this.Cursor = Cursors.Default;
            if (treeView1.Nodes.Count == 0)
            {
                MessageBox.Show("Non ci sono files da copiare", "CopyIT", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tsbExit_Click(this, new EventArgs());

            }
            tsbCopy.Enabled = true;
            tsbExit.Enabled = true;
            tsbSelectAll.Enabled = true;
        }

        void sincronizer_BeginSyncronize(object sender, EventArgs e)
        {
            tsbStop.Visible = true;
            tsbCopy.Enabled = false;
            tsbExit.Enabled = false;
            tsbSelectAll.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
        }

        void sincronizer_FileSyncronize(object sender, SyncronizerEventArgs e)
        {
            tssLabel.Text = string.Format("File Analizzati: {0:#####0}", e.FileAnalized); ;
            tssTotalFile.Text = string.Format("File Trovati: {0:#####0}", e.FileFound);
        }

        public void Sincronize(List<PairsParameters> pairs)
        {
            sincronizer.FindFiles(pairs);
        }

        public void Sincronize(PairsParameters pairs)
        {
            sincronizer.FindFiles(pairs);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            if (Close != null)
                Close(this, e);
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            this.treeView1.AfterCheck -= new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

            bool recursive = e.Node.Checked && (treeView1.Tag != null && treeView1.Tag.ToString() == "True");
            EnableDisableTree(e.Node, e.Node.Checked, recursive);
            if (e.Node.Checked)
                EnableParent(e.Node);
            else
                DisableParent(e.Node);

            this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

        }

        private void tsbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            this.treeView1.AfterCheck -= new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

            EnableDisableTree(treeView1.Nodes[0], !tsbSelectAll.Checked, true);

            this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

            tsbSelectAll.Image = tsbSelectAll.Checked ? global::CopyIt.Properties.Resources.Select :
                global::CopyIt.Properties.Resources.Deselect;
        }

        private void EnableDisableTree(TreeNode currNode, bool state, bool recursive)
        {
            foreach (TreeNode node in currNode.Nodes)
            {
                if ((node.Nodes.Count > 0 && recursive) || !state)
                {
                    EnableDisableTree(node, state, recursive);
                    node.Checked = state;
                }
                else
                {
                    if (node.Nodes.Count == 0)
                        node.Checked = state && node.Nodes.Count == 0;
                }
            }
            currNode.Checked = state;
        }

        private void EnableParent(TreeNode node)
        {
            if (node.Parent != null)
            {
                node.Parent.Checked = true;
                EnableParent(node.Parent);
            }
        }

        private void DisableParent(TreeNode node)
        {
            bool nodeCheck = false;
            if (node.Parent != null)
            {
                foreach (TreeNode cur in node.Parent.Nodes)
                    nodeCheck |= cur.Checked;
            }

            if (!nodeCheck && node.Parent != null)
            {
                node.Parent.Checked = false;
                DisableParent(node.Parent);
            }
        }

        private void treeView1_KeyDown(object sender, KeyEventArgs e)
        {
            treeView1.Tag = e.Control.ToString();
        }

        private void treeView1_KeyUp(object sender, KeyEventArgs e)
        {
            treeView1.Tag = null;
        }

        private void DisplayPreview_KeyUp(object sender, KeyEventArgs e)
        {
            treeView1.Tag = e.Control.ToString();
        }

        private void DisplayPreview_KeyDown(object sender, KeyEventArgs e)
        {
            treeView1.Tag = null;
        }

        private void tsbCopy_Click(object sender, EventArgs e)
        {
            if (Copy != null)
                Copy(this, e);
        }

        private void tsbStop_Click(object sender, EventArgs e)
        {
            sincronizer.StopSyncronize();
        }
    }
}
